package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCrypt;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


/**
 * @author Zwnan
 */
@Service
public class DtsAdminServiceImpl implements DtsAdminService {


    @Autowired
    private DtsAdminMapper dtsAdminMapper;


    @Override
    public DtsAdmin findByUsername(String username) {

        return dtsAdminMapper.selectOne(new QueryWrapper<DtsAdmin>()
                .eq("username", username)
                .eq("deleted", false));
    }

    @Override
    public List<DtsAdmin> findAll() {
        QueryWrapper<DtsAdmin> queryWrapper = new QueryWrapper<DtsAdmin>();
        queryWrapper.eq("deleted", false);

        return dtsAdminMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<DtsAdmin> selectByPage(String username, Integer page, Integer limit, String sort, String order) {
        IPage<DtsAdmin> iPage = new Page<>(page,limit);

        QueryWrapper<DtsAdmin> wapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            wapper.eq("username", username);
        }
        wapper.eq("deleted", false);


        return dtsAdminMapper.selectPage(iPage,wapper);
    }

    @Override
    public void update(DtsAdmin dtsAdmin) {
        dtsAdmin.setPassword(new BCryptPasswordEncoder().encode(dtsAdmin.getPassword()));
        dtsAdmin.setUpdateTime(new Date());
        dtsAdminMapper.updateById(dtsAdmin);
    }

    @Override
    public void insert(DtsAdmin dtsAdmin) {
        dtsAdmin.setPassword(new BCryptPasswordEncoder().encode(dtsAdmin.getPassword()));
        dtsAdminMapper.insert(dtsAdmin);
    }

    @Override
    public void delete(DtsAdmin dtsAdmin) {
        dtsAdmin.setDeleted(true);
        dtsAdminMapper.updateById(dtsAdmin);
    }

    @Override
    public List<CatVo> findName() {


        return dtsAdminMapper.findName();
    }


}
